﻿package plugin.component
{
    import com.core.*;
    import com.core.display.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.text.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import view.tips.*;

    public class FlashItem extends Sprite implements IComponent
    {
        private var _oldData:Object = null;
        private var _radio:Number = 0;
        private var _iconBm:Bitmap;
        private var _bgBm:Bitmap;
        private var _bgBm1:Bitmap;
        private var _countTxt:TextField;
        private var _effect:TipsEffect;
        private var _hasBagCell:Boolean = false;
        private var _hasBagCell1:Boolean = false;
        private var _hasEff:Boolean = false;
        private var _hasText:Boolean = false;
        private var _hasEvent:Boolean = false;
        private var _txtPoint:Point = null;
        private var _initData:Object = null;
        public var effshow:Boolean = false;
        private var mFlagIcon:Bitmap;
        private var superbox:Container;
        private var _curEff:int = 0;

        public function FlashItem(param1:Object = null, param2:Number = 0.6, param3:Boolean = false, param4:Boolean = false, param5:Point = null, param6:Boolean = true, param7:Boolean = false, param8:Boolean = false)
        {
            this._initData = param1;
            this._radio = param2;
            this._hasBagCell = param3;
            this._hasText = param4;
            this._hasEvent = param6;
            this._txtPoint = param5;
            this._hasBagCell1 = param7;
            this.effshow = param8;
            this.initView();
            return;
        }// end function

        protected function initView() : void
        {
            var _loc_2:TextFormat = null;
            if (this._hasBagCell1)
            {
                this._bgBm1 = new Bitmap(App.asset.getBitmapData("png.common.bagcell2"));
                this._bgBm1.x = -2;
                this._bgBm1.y = -2;
                this.addChild(this._bgBm1);
            }
            if (this._hasBagCell)
            {
                this._bgBm = new Bitmap(App.asset.getBitmapData("png.common.bagcell"));
                this._bgBm.scaleX = this._radio;
                this._bgBm.scaleY = this._radio;
                this.addChild(this._bgBm);
            }
            var _loc_1:* = App.asset.getBitmapData("png.common.noramlicon");
            this._iconBm = new Bitmap(_loc_1);
            this._iconBm.scaleX = this._radio;
            this._iconBm.scaleY = this._radio;
            this._iconBm.x = 4 * this._radio;
            this._iconBm.y = 4 * this._radio;
            this.addChild(this._iconBm);
            if (this._hasText)
            {
                this._countTxt = new TextField();
                _loc_2 = new TextFormat();
                _loc_2.align = TextFormatAlign.RIGHT;
                _loc_2.size = 12;
                _loc_2.font = "SimSun";
                this._countTxt.autoSize = TextFieldAutoSize.LEFT;
                this._countTxt.defaultTextFormat = _loc_2;
                this._countTxt.text = "";
                this._countTxt.wordWrap = true;
                this._countTxt.width = 67 * this._radio;
                this._countTxt.selectable = false;
                this._countTxt.mouseEnabled = false;
                this.addChild(this._countTxt);
                if (this._txtPoint)
                {
                    this._countTxt.x = this._txtPoint.x;
                    this._countTxt.y = this._txtPoint.y;
                }
                else
                {
                    this._countTxt.y = 60 * this._radio;
                }
            }
            if (this._hasEvent)
            {
                this.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            }
            if (this._initData)
            {
                this.data = this._initData;
                this._initData = null;
            }
            this.superbox = new Container();
            this.superbox.name = "superbox";
            this.addChild(this.superbox);
            this.setQianghuaLv();
            this.setEff();
            this.setShenZhuanIcon();
            return;
        }// end function

        private function setShenZhuanIcon() : void
        {
            if (!this._oldData)
            {
                return;
            }
            var _loc_1:* = this.getChildByName("sz") as Image;
            if (this._oldData.suitid > 0)
            {
                if (!_loc_1)
                {
                    if (this._oldData.suitid < 100)
                    {
                        _loc_1 = new Image("png.common.shenzhuang");
                    }
                    else
                    {
                        _loc_1 = new Image("png.common.rexie");
                    }
                    _loc_1.name = "sz";
                    _loc_1.x = 0;
                    _loc_1.y = 0;
                    this.addChild(_loc_1);
                }
                else if (this._oldData.suitid < 100)
                {
                    _loc_1.url = "png.common.shenzhuang";
                }
                else
                {
                    _loc_1.url = "png.common.rexie";
                }
            }
            else if (_loc_1)
            {
                this.removeChild(_loc_1);
                _loc_1 = null;
            }
            return;
        }// end function

        private function setQianghuaLv() : void
        {
            var _loc_1:Label = null;
            if (!this._oldData)
            {
                return;
            }
            if (this._oldData.itemtype == 2)
            {
                return;
            }
            if (this._radio == 1)
            {
                this.superbox.x = 3 + 20;
            }
            else
            {
                this.superbox.x = 3;
            }
            this.superbox.y = -1;
            this.superbox = this.getChildByName("superbox") as Container;
            if (!this.superbox)
            {
                this.superbox = new Container();
                this.superbox.name = "superbox";
                this.addChild(this.superbox);
            }
            while (this.superbox.numChildren > 0)
            {
                
                this.superbox.removeChildAt(0);
            }
            if (this._oldData.superlevel > 0)
            {
                _loc_1 = new Label();
                _loc_1.color = MColor.New62;
                _loc_1.width = 40;
                _loc_1.height = 22;
                _loc_1.x = 0;
                _loc_1.y = -3;
                _loc_1.font = "Simsun";
                _loc_1.align = "right";
                _loc_1.stroke = "0x0";
                _loc_1.bold = true;
                if (this._radio == 1)
                {
                    _loc_1.size = 15;
                }
                else
                {
                    _loc_1.size = 12;
                }
                _loc_1.text = "+" + this._oldData.superlevel;
                this.superbox.addChild(_loc_1);
            }
            return;
        }// end function

        protected function onOver(event:MouseEvent) : void
        {
            if (!this._oldData)
            {
                return;
            }
            this.removeEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            if (this._oldData.itemtype == 1)
            {
                ModelProxy.change("tips", "arm", {data:this._oldData, p:new Point(GlobalLayer.instance.flashStage.mouseX, GlobalLayer.instance.flashStage.mouseY)});
            }
            else
            {
                ModelProxy.change("tips", "tool", {data:this._oldData, p:new Point(GlobalLayer.instance.flashStage.mouseX, GlobalLayer.instance.flashStage.mouseY)});
            }
            this.addEventListener(MouseEvent.MOUSE_OUT, this.onOut);
            return;
        }// end function

        protected function onOut(event:MouseEvent) : void
        {
            this.removeEventListener(MouseEvent.MOUSE_OUT, this.onOut);
            ModelProxy.change("tips", "state", "close");
            this.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            return;
        }// end function

        public function set data(param1:Object) : void
        {
            var _loc_5:BitmapData = null;
            var _loc_2:* = param1.flag;
            if (_loc_2 && _loc_2.length > 0 && int(_loc_2) > 0)
            {
                if (!this.mFlagIcon)
                {
                    this.mFlagIcon = new Bitmap();
                    this.mFlagIcon.scaleX = this._radio;
                    this.mFlagIcon.scaleY = this._radio;
                    if (_loc_2 == "3")
                    {
                        this.mFlagIcon.x = 0;
                        this.mFlagIcon.y = 0;
                    }
                    else
                    {
                        this.mFlagIcon.x = this.width - 42;
                        this.mFlagIcon.y = 0;
                    }
                    this.mFlagIcon.smoothing = true;
                    addChild(this.mFlagIcon);
                }
                _loc_5 = App.asset.getBitmapData("png.common.itemflag_" + _loc_2);
                if (_loc_5)
                {
                    this.mFlagIcon.bitmapData = _loc_5;
                    this.mFlagIcon.smoothing = true;
                }
            }
            else if (this.mFlagIcon)
            {
                if (this.mFlagIcon.parent)
                {
                    this.mFlagIcon.parent.removeChild(this.mFlagIcon);
                }
                this.mFlagIcon = null;
            }
            if (this._oldData)
            {
                if (this._oldData.itemid != param1.itemid)
                {
                }
                else
                {
                    if (!this._hasText)
                    {
                        return;
                    }
                    if (param1.count != null)
                    {
                        if (param1.needCount != null)
                        {
                            if (param1.needCount > param1.count)
                            {
                                this._countTxt.htmlText = "<font color=\"#ff0000\">" + param1.count + "/" + param1.needCount + "</font>";
                            }
                            else
                            {
                                this._countTxt.htmlText = "<font color=\"#00ff00\">" + param1.count + "/" + param1.needCount + "</font>";
                            }
                        }
                        else
                        {
                            this._countTxt.htmlText = "<font color=\"#ffffff\">" + this.getCounts(param1.count) + "</font>";
                        }
                    }
                    else
                    {
                        this._countTxt.htmlText = "";
                    }
                    return;
                }
            }
            this._oldData = param1;
            if (param1.icon == "")
            {
                this._oldData.icon = "";
            }
            else
            {
                this._oldData = param1;
            }
            var _loc_3:* = UIGlobel.getItemd(1);
            var _loc_4:* = _loc_3 + this._oldData.icon + Config.IconExtension;
            App.mloader3.loadBMD(Config.getUrl(_loc_4, ResourceType.NORMAL), 1, new Handler(this.onComp));
            if (!this._hasText)
            {
                return;
            }
            if (param1.count != null)
            {
                if (param1.needCount != null)
                {
                    if (param1.needCount > param1.count)
                    {
                        this._countTxt.htmlText = "<font color=\"#ff0000\">" + param1.count + "/" + param1.needCount + "</font>";
                    }
                    else
                    {
                        this._countTxt.htmlText = "<font color=\"#00ff00\">" + param1.count + "/" + param1.needCount + "</font>";
                    }
                }
                else
                {
                    this._countTxt.htmlText = "<font color=\"#ffffff\">" + this.getCounts(param1.count) + "</font>";
                }
            }
            else
            {
                this._countTxt.htmlText = "";
            }
            if (!this._txtPoint)
            {
                this._countTxt.y = 60 * this._radio - this._countTxt.height;
            }
            return;
        }// end function

        private function getCounts(param1:int) : String
        {
            return param1 / 10000 > 1 ? ((param1 / 10000).toFixed(1) + "万") : (param1 + "");
        }// end function

        public function getFilter() : ColorMatrixFilter
        {
            var _loc_1:Array = [0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0];
            return new ColorMatrixFilter(_loc_1);
        }// end function

        private function onComp(param1:BitmapData) : void
        {
            if (this._iconBm)
            {
                if (param1)
                {
                    this._iconBm.bitmapData = param1;
                }
                else
                {
                    this._iconBm.bitmapData = null;
                }
                this._iconBm.smoothing = true;
                if (this._oldData && this._oldData.gary)
                {
                    this._iconBm.filters = [this.getFilter()];
                }
                else
                {
                    this._iconBm.filters = null;
                }
            }
            return;
        }// end function

        private function initEff(param1:int) : void
        {
            return;
        }// end function

        public function dispose() : void
        {
            if (this._effect)
            {
                if (this._effect.parent)
                {
                    this._effect.parent.removeChild(this._effect);
                }
                this._effect.dispose();
                this._effect = null;
            }
            if (this._countTxt)
            {
                if (this._countTxt.parent)
                {
                    this._countTxt.parent.removeChild(this._countTxt);
                }
                this._countTxt = null;
            }
            if (this._bgBm)
            {
                if (this._bgBm.parent)
                {
                    this._bgBm.parent.removeChild(this._bgBm);
                }
                this._bgBm = null;
            }
            if (this._hasEvent)
            {
                if (this.hasEventListener(MouseEvent.MOUSE_OVER))
                {
                    this.removeEventListener(MouseEvent.MOUSE_OVER, this.onOver);
                }
                if (this.hasEventListener(MouseEvent.MOUSE_OUT))
                {
                    this.removeEventListener(MouseEvent.MOUSE_OUT, this.onOver);
                }
            }
            if (this._iconBm)
            {
                if (this._iconBm.parent)
                {
                    this._iconBm.parent.removeChild(this._iconBm);
                }
                this._iconBm = null;
            }
            if (this.mFlagIcon)
            {
                if (this.mFlagIcon.parent)
                {
                    this.mFlagIcon.parent.removeChild(this.mFlagIcon);
                }
                this.mFlagIcon = null;
            }
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            return;
        }// end function

        public function get comXml() : Object
        {
            return null;
        }// end function

        public function set comXml(param1:Object) : void
        {
            return;
        }// end function

        private function setEff() : void
        {
            var _loc_1:int = 0;
            var _loc_2:TipsEffect = null;
            var _loc_3:int = 0;
            var _loc_4:Number = NaN;
            if (this.effshow)
            {
                _loc_1 = this._oldData.quality;
                if (this._curEff == _loc_1)
                {
                    return;
                }
                this._curEff = _loc_1;
                _loc_2 = this.getChildByName("effect") as TipsEffect;
                if (_loc_2)
                {
                    if (_loc_1 > 1)
                    {
                        _loc_2.visible = true;
                        _loc_2.show(_loc_1);
                    }
                    else
                    {
                        this.removeChild(_loc_2);
                        _loc_2.dispose();
                        _loc_2 = null;
                    }
                }
                else if (_loc_1 > 1)
                {
                    _loc_2 = new TipsEffect(_loc_1);
                    _loc_2.mouseChildren = false;
                    _loc_2.mouseEnabled = false;
                    _loc_2.name = "effect";
                    _loc_2.show(_loc_1);
                    _loc_3 = 100;
                    _loc_4 = 1;
                    if (this._iconBm)
                    {
                        _loc_4 = this._iconBm.width / 60;
                        _loc_2.setSize(_loc_4, (-(512 - _loc_3)) * _loc_4 / 2 - 5, (-(512 - _loc_3)) * _loc_4 / 2 - 5);
                        _loc_2.x = this._iconBm.x;
                        _loc_2.y = this._iconBm.y;
                    }
                    this.addChild(_loc_2);
                }
            }
            return;
        }// end function

    }
}
